package com.igd.listener;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.igd.business.dao.IBaseDao;
import com.igd.common.constant.SessionConstant;
import com.igd.exceptions.IgdException;
import com.igd.security.Resources;

 /**
 * @version  1.0
 *
 * @author   QIAOYU
 *
 * @date     2013-6-25
 *
 * @description	 Web应用初始化listener,初始化平台资源以及平台全局属性
 */

public class WebInitListener implements ServletContextListener {

	private final String BASE_DAO_ID = "baseDao";
	
	private final String GET_RESOURCE_SQL = "user.getAllResource";
	
	private Logger logger = Logger.getLogger(WebInitListener.class);
	
	public void contextInitialized(ServletContextEvent sce) {
		
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
		
		if(null != context){
			
			IBaseDao baseDao = (IBaseDao) context.getBean(BASE_DAO_ID);
			
			try {
				
				/** 获取系统平台所有的根节点菜单 */
				List<Resources> resList = baseDao.queryForList(GET_RESOURCE_SQL,new Resources());
				
				ServletContext application = sce.getServletContext();
				
				application.setAttribute(SessionConstant.ROOT_MENU_KEY,resList);
				/** 获取系统平台所有的根节点菜单 */
				
			} catch (SQLException e) {
				
				logger.error(e.getMessage());
				
			} catch (IgdException e) {
				
				logger.error(e.getMessage());

			}
		}
	}
	
	public void contextDestroyed(ServletContextEvent sce) {
		
	}
}
